package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class CellIcon extends JPanel implements MouseListener{

	private static final long serialVersionUID = 2141234930632816998L;

	protected enum State
	{
		LIT, PHANTOM, DARK, HOVER;
		private Color getColor()
		{	
			switch (this)
			{
			case LIT:
				return Color.BLUE;
			case PHANTOM:
				return new Color(0, 0, 65);
			case DARK:
				return Color.DARK_GRAY;
			case HOVER:
				return Color.GRAY;	
			}
			return Color.DARK_GRAY;
		}
	};
	
	//this cells position on the table
	private int pos_x, pos_y;
	
	protected State state;

	public CellIcon(int x, int y)
	{
		this.pos_x = x;
		this.pos_y = y;
		this.state = State.DARK;
		
		this.addMouseListener(this);
		
		this.setSize(50, 50);
		
		this.setLocation(5+55*pos_x, 5+55*pos_y);
		
		//this.setBackground(Color.DARK_GRAY);
		this.setVisible(true);
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D) g;
		
		g2.setColor(this.state.getColor());
		g2.fillRect(0, 0, 50, 50);
	}

	//TODO enable dragging
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		//System.out.println("clicked");
		this.setState(State.LIT);
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//System.out.println("entered");
		this.hover();
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		this.hover();
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//System.out.println("mouse pressed");
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private void hover() {
		if(this.state == State.DARK)	//TODO enable hover while phantom
		{
			this.setState(State.HOVER);
		}
		else if(this.state == State.HOVER)
		{
			this.setState(State.DARK);
		}
	}

	protected void setState(State s) {
		//System.out.println("setting state "+s.toString());
		this.state = s;
		this.repaint();
		//TODO this.notifyObservers();
		
	}

	public boolean getIsSelected() {
		return this.state == State.LIT;
	}

	
}
